package cn.wangxl.vertx.starter.web.handlebody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/**
 * body
 *
 */
public class RequestJsonBody extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    router.post("/json")
      .handler(BodyHandler.create().setBodyLimit(1))// 限制请求体的大小
      .handler(ctx->{
      HttpServerRequest request = ctx.request();
      HttpServerResponse response = ctx.response();



      if(request.getHeader("Content-Type").equalsIgnoreCase("application/json")){
        JsonObject body = ctx.getBodyAsJson();
        System.out.println(body);

        // 接受jsonbody并转换成可用的对象
        String bodyStr = ctx.getBody().toString();
        ObjectMapper objectMapper = new ObjectMapper();

        BaseBO baseBO = null;
        try {
          baseBO = objectMapper.readValue(bodyStr,BaseBO.class);
          ctx.put("data",baseBO);
        } catch (JsonProcessingException e) {
          e.printStackTrace();
        }
        try {
          response.end(objectMapper.writeValueAsString(baseBO));
        } catch (JsonProcessingException e) {
          e.printStackTrace();
        }
      }
      response.end("OK");
    });


    router.post("/formdata")
      .handler(BodyHandler.create().setBodyLimit(1).setMergeFormAttributes(true))// 限制请求体的大小
      .handler(ctx->{
        HttpServerRequest request = ctx.request();
        HttpServerResponse response = ctx.response();

        ctx.getBody();
        if(request.getHeader("Content-Type").equalsIgnoreCase("application/json")){
          JsonObject body = ctx.getBodyAsJson();
          System.out.println(body);

          // 接受jsonbody并转换成可用的对象
          String bodyStr = ctx.getBody().toString();
          ObjectMapper objectMapper = new ObjectMapper();

          BaseBO baseBO = null;
          try {
            baseBO = objectMapper.readValue(bodyStr,BaseBO.class);
            ctx.put("data",baseBO);
          } catch (JsonProcessingException e) {
            e.printStackTrace();
          }
          try {
            response.end(objectMapper.writeValueAsString(baseBO));
          } catch (JsonProcessingException e) {
            e.printStackTrace();
          }
        }
        response.end("OK");
      });

    server.requestHandler(router).listen(9999, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }


  public static void main(String[] args) throws Exception {
    new RequestJsonBody().start(Future.future());
  }
}
